package com.hegaoye.mqhandler;

import com.hegaoye.base.RabbitmqConfig;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * Created by lixin on 2019/6/29.
 */
@Slf4j
@Component
public class TestConfirmReceiver {

    @RabbitListener(queues = RabbitmqConfig.Test_QUEUE_NAME)
    public void process1(Message message, Channel channel) throws IOException {
        long tag = message.getMessageProperties().getDeliveryTag();
        try {
            channel.basicAck(tag, true);
        } catch (IOException e) {
            channel.basicNack(tag, false, true);
            e.printStackTrace();
        }
        log.info("test1 >> " + new String(message.getBody()));
    }

    @RabbitListener(queues = RabbitmqConfig.Test_QUEUE_NAME)
    public void process2(Message message, Channel channel) throws IOException {
        long tag = message.getMessageProperties().getDeliveryTag();
        try {
            Thread.sleep(100);
            channel.basicAck(tag, true);
        } catch (IOException e) {
            channel.basicNack(tag, false, true);
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("test2 >> " + new String(message.getBody()));
    }
}
